$(document).ready(function () {
	
	// =========================================================
	// Tabs
	// =========================================================
	
	// Switching tabs in index.html
	// Reference: http://www.red-team-design.com/css3-jquery-folder-tabs
	$("#tab_content div").hide(); // Initially hide all content
	$("#tabs li:first").attr("id","current"); // Activate first tab
	$("#tab_content div:first").fadeIn(); // Show first tab content
	$("#tab_content div:first div").show();
	$("#tab_content div:first .project_editing").hide();
	$("#tab_content div:first .project_deleting").hide();
	$("#tabs a").click(function(e) {
	    e.preventDefault();
	    if ($(this).closest("li").attr("id") == "current") { //detection for current tab
	    	return       
	    } else {             
		    $("#tab_content div").hide(); //Hide all content
		    $("#tabs li").attr("id",""); //Reset id's
		    $(this).parent().parent().attr("id","current"); // Activate this
		    $('#' + $(this).attr('name')).fadeIn(); // Show content for current tab
		    
		    $('#' + $(this).attr('name') + " div").show();
		    $('#' + $(this).attr('name') + " .project_editing").hide();
		    $('#' + $(this).attr('name') + " .project_deleting").hide();
		    $('#' + $(this).attr('name') + " .sprint_deleting").hide();
		    $(".edit").removeClass("active");
		    $(".delete").removeClass("active");
	    }
	    
	    // Always show input boxes
	    $(".input_box").show();
	});
	
	// =========================================================
	// Collapsible panels
	// =========================================================
	
	$(".edit").click(function (event) {
		$(".project_deleting").slideUp();
		$(".project_editing").not($(this).parent().parent().next(".project_editing")).slideUp();
		
		$(".edit").removeClass("active");
		$(".delete").removeClass("active");
		
		if (!$(this).parent().parent().next(".project_editing").is(":visible")) {
			$(this).addClass("active");
		}
		$(this).parent().parent().next(".project_editing").slideToggle("slow");
	});
	
	$(".delete").click(function (event) {
		$(".project_editing").slideUp();
		$(".project_deleting").not($(this).parent().parent().next().next(".project_deleting")).slideUp();
		
		$(".edit").removeClass("active");
		$(".delete").removeClass("active");
		
		if (!$(this).parent().parent().next().next(".project_deleting").is(":visible")) {
			$(this).addClass("active");
		}
		$(this).parent().parent().next().next(".project_deleting").slideToggle("slow");
	});
	
	
	// =========================================================
	// Dialogs
	// =========================================================

	var title = $("#title"),
    	content = $("#content"),
    	tests = $("#tests"),
    	value = $("#value"),
    	allFields = $([]).add(title).add(content).add(tests).add(value);
	    
	$(".edit_story").click(function (event) {
		event.preventDefault();
		
		title.val($(this).prev().prev().prev().prev().text());
		content.val($(this).parent().next().text());
		var breaks = new RegExp("<br>","g")
		tests.val($(this).parent().next().next().html().replace(breaks, "\n"));
		
		$("#priority").find("option:selected").prop('selected', false);
		var priority_text = $(this).prev().prev().find(".priority").text();
		$("#priority option[value='"+priority_text+"']").prop('selected', true);
		
		value.val($(this).prev().prev().find(".value").text());
		
		$(this).attr('id', 'trigger_id');		
		$("#edit_dialog").dialog("open");
	});
	      
	$( "#edit_dialog" ).dialog({
		autoOpen: false,
	    height: 760,
	    width: 650,
	    modal: true,
	    buttons: {
	    	"Confirm": function() {
	         	// Vzemi stvari iz polj in jih zapisi spremenjene v bazo...
	         	var story_id = $("#trigger_id").parent().parent().find("input:hidden").val()
	         	
	         	var title = $('#title').val();
	         	var content = $('#content').val();
	     	    var breaks = new RegExp("\n","g")
	         	var tests = $('#tests').val().replace(breaks, "<br/>");
	         	var priority = $("#priority").find("option:selected").val();
	         	var value = $('#value').val();
	         	
	         	$("#trigger_id").prev().prev().prev().prev().html(title);
	         	$("#trigger_id").parent().next().html(content);
	         	$("#trigger_id").parent().next().next().html(tests);
	         	$("#trigger_id").prev().prev().find(".priority").html(priority);
	         	$("#trigger_id").prev().prev().find(".value").html(value);
	         	
	            $(this).dialog("close");
	        },
	        Cancel: function() {
	          	$(this).dialog("close");
	        }
	    },
	    close: function() {
	    	$("#trigger_id").attr('id', '');
	      	$(".edit").removeClass("active");
	        allFields.val("");
	    }
	});
	
	$(".delete_story").click(function (event) {
		event.preventDefault();
		$(this).attr('id', 'trigger_id');
		$("#delete_dialog").dialog("open");
	});
	
	$("#delete_dialog").dialog({
		autoOpen: false,
	    height: 200,
	    width: 650,
	    modal: true,
	    buttons: {
	    	"Delete": function() {
	         	// Izbrisi zgodbo iz baze z AJAX klicem in nato odstrani iz HTML...
	         	var story_id = $("#trigger_id").parent().parent().find("input:hidden").val()
	         	
	         	$("#trigger_id").parent().parent().remove();
	         	
	            $(this).dialog("close");
	        },
	        Cancel: function() {
	          	$(this).dialog("close");
	        }
	    },
	    close: function() {
	    	$("#trigger_id").attr('id', '');
	      	$(".edit").removeClass("active");
	        allFields.val("");
	    }
	});
	
	$(".notes_story").click(function (event) {
		event.preventDefault();
		$(this).attr('id', 'trigger_id');
		$('#notes').val($(this).parent().parent().find(".comments").text());
		$("#notes_dialog").dialog("open");
	});
	
	$( "#notes_dialog" ).dialog({
		autoOpen: false,
	    height: 310,
	    width: 650,
	    modal: true,
	    buttons: {
	    	"Save": function() {
	         	// Shrani notes...
	         	var story_id = $("#trigger_id").parent().parent().find("input:hidden").val()
	         	var notes = $('#notes').val();
	         	
	         	if (notes == "") {
	         		$("#trigger_id").parent().parent().find(".comment_separator").addClass("hidden");
	         		$("#trigger_id").parent().parent().find(".comments").addClass("hidden");
	         		$("#trigger_id").parent().parent().find(".comments").html("");
	         	} else {
	         		$("#trigger_id").parent().parent().find(".comment_separator").removeClass("hidden");
	         		$("#trigger_id").parent().parent().find(".comments").removeClass("hidden");
	         		$("#trigger_id").parent().parent().find(".comments").html(notes);
	         	}
	         	
	            $(this).dialog("close");
	        },
	        Cancel: function() {
	          	$(this).dialog("close");
	        }
	    },
	    close: function() {
	    	$("#trigger_id").attr('id', '');
	      	$(".edit").removeClass("active");
	        allFields.val("");
	    }
	});
	
	$(".poker_story").click(function (event) {
		event.preventDefault();
		$("#poker_dialog").dialog("open");
	});
	
	$( "#poker_dialog" ).dialog({
		autoOpen: false,
	    height: 800,
	    width: 650,
	    modal: true,
	    buttons: [
	    	{
	    		text: "New round",
	    		width: 200,
	    		click: function() {
	    			$(".ui-dialog-buttonpane button:contains('New round') span").text("End round");
	        	}
	        },
	        {
	        	text: "Use estimate",
	        	width: 200,
	        	click: function() {
	         		$(this).dialog("close");
	        	}
	        }
	    ],
	    close: function() {
	      	$(".edit").removeClass("active");
	        allFields.val("");
	    }
	});
	
	function isNumber(n) {
	  	return !isNaN(parseFloat(n)) && isFinite(n);
	}
	
	$("#poker_time_estimate_submit").click(function (event) {
		event.preventDefault();
		var estimate = $("#poker_time_estimate").val();
		$("#current_estimate").html(estimate);
		
		var sum = 0;
		$( ".current_round" ).each(function( index ) {
			if (isNumber(parseInt($(this).text(), 10))) {
				sum = sum + parseInt($(this).text(), 10);
			}
		});
		$("#current_average").html(sum);
	});
	
	
	// =========================================================
	// Brisanje projektov in uporabnikov
	// =========================================================
	
	$('.delete_project').click(function (event) {
		event.preventDefault();
		
		var project_id = $(this).parent().prev().prev().find('input:hidden').val();
		
		// AJAX call to delete from base with project_id
		// ...
		
		// Remove from html if deleting in AJAX succesfull 
	    $(this).parent().prev().prev().remove();
	    $(this).parent().prev().remove();
	    $(this).parent().remove();
	    
	    // =========================================================
	    // !!!!!   PRIMER KAKO SE UPORABLJA TO Z AJAX KLICEM   !!!!!
	    // =========================================================
	    // ISTI PRINCIP ZA VSE TAKE STVARI
	    
//	    var project_id = $(this).parent().prev().prev().find('input:hidden').val();
//	    
	    // For later reference in AJAX call
//	    var $remove1 = $(this).parent().prev().prev().remove();
//	    var $remove2 = $(this).parent().prev().remove();
//	    var $remove3 = $(this).parent().remove();
//	    		
	    // Create an AJAX call to add a feed to database (http://www.w3schools.com/php/php_ajax_database.asp)
//	    if (window.XMLHttpRequest)
//	    	xmlhttp=new XMLHttpRequest();
//	    else
//	    	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
//	    	
//	    xmlhttp.onreadystatechange=function() {
//	    	if (xmlhttp.readyState==4 && xmlhttp.status==200) {
	    		// Odstrani
//	    		$remove1.remove();
//	    		$remove2.remove();
//	    		$remove3.remove();
//	    	}
//	    }
//	    xmlhttp.open("GET","SKRIPTA.php?PROJECT_ID="+project_id,true);
//	    xmlhttp.send();
	});
	
	$('.delete_user').click(function (event) {
		event.preventDefault();
		
		var user_id = $(this).parent().prev().prev().find('input:hidden').val();
		
		// AJAX call to delete from base with user_id
		// ...
		
		// Remove from html if deleting in AJAX succesfull
	    $(this).parent().prev().prev().remove();
	    $(this).parent().prev().remove();
	    $(this).parent().remove();
	});
	
	$('.sprint_delete').click(function (event) {
		event.preventDefault();
		
		var sprint_id = $(this).parent().prev().find('input:hidden').val();
		
		// AJAX call to delete from base with user_id
		// ...
		
		// Remove from html if deleting in AJAX succesfull
	    $(this).parent().prev().remove();
	    $(this).parent().remove();
	});
	
	// =========================================================
	// Editiranje projektov in uporabnikov
	// =========================================================
	
	$(".edit_user").click(function (event) {
		event.preventDefault();
		
		// Get all vars
		var username = $('input[name=username]', $(this).parent()).val();
		var email = $('input[name=email]', $(this).parent()).val();
		
		// AJAX call to save changes in db
		// ...
		
		// Dynamically edit page if AJAX succesfull
		$(this).parent().parent().prev().find('.left').html("<h4>"+username+"</h4>, "+email);
		
		$(".edit").removeClass("active");
		$(".project_editing").slideUp();
	});
	
	$(".edit_project").click(function (event) {
		event.preventDefault();
		
		// Get all vars
		var name = $('input[name=name]', $(this).parent()).val();
		var leader = $('input[name=leader]', $(this).parent()).val();
		
		// AJAX call to save changes in db
		// ...
		
		// Dynamically edit page if AJAX succesfull
		$(this).parent().parent().prev().find('.left').html("<h4>"+name+"</h4>, "+leader);
		
		$(".edit").removeClass("active");
		$(".project_editing").slideUp();
	});
	
	$(".right_button").click(function (event) {
		$(".edit").removeClass("active");
		$(".delete").removeClass("active");
		$(".delete_sprint").removeClass("active");
		
		$(".project_deleting").slideUp();
		$(".project_editing").slideUp();
		$(".sprint_deleting").slideUp();
	});
	
	// =========================================================
	// Editiranje time estimate user stories
	// =========================================================
	
	$(".time_estimate").focusout(function (event) {
		var story_id = $(this).parent().parent().parent().find("input:hidden").val();
		
		// Use AJAX to save new time estimate!
	});
	
	$(".select_proj").click(function (event) {
		event.preventDefault();
		
		$(".select_proj").removeClass("active");
		$(".select_proj").html("select");
		$(this).addClass("active");
		$(this).html("selected");
	});
	
	// =========================================================
	// Editiranje time estimate user stories
	// =========================================================
	
	$(".velocity").focusout(function (event) {
		var sprint_id = $(this).parent().parent().find("input:hidden").val();
		
		// Use AJAX to save new time estimate!
	});
	
	$(".delete_sprint").click(function (event) {
		$(".sprint_deleting").not($(this).parent().parent().next(".sprint_deleting")).slideUp();
		$(".delete_sprint").removeClass("active");
		
		if (!$(this).parent().parent().next(".sprint_deleting").is(":visible")) {
			$(this).addClass("active");
		}
		$(this).parent().parent().next(".sprint_deleting").slideToggle("slow");
	});
	
	$('.sprint_delete').click(function (event) {
		event.preventDefault();
		
		var sprint_id = $(this).parent().prev().find('input:hidden').val();
		
		// AJAX call to delete from base with user_id
		// ...
		
		// Remove from html if deleting in AJAX succesfull
	    $(this).parent().prev().remove();
	    $(this).parent().remove();
	});
	
	// =========================================================
	// Dodajanje zgodb v sprint
	// =========================================================
	
	$('.add_to_sprint_button').click(function (event) {
		event.preventDefault();
		
		var sprint_id = $(this).parent().parent().find('input:hidden').val();
		
		// AJAX call to add to Sprint
		// ...

	});
	
	// =========================================================
	// Tasks
	// =========================================================
	
	$('.accept_task').click(function (event) {
		event.preventDefault();
		
		var taskNameToAccept = $(this).parent().prev().html();
		
		// Shrani member za task name
			
		$(this).parent().next().html($("#username").html());
		$(this).parent().html("Assigned");
	});
	
	$('.release_task').click(function (event) {
		event.preventDefault();
		
		var taskNameToRelease = $(this).parent().prev().prev().html();
		$(this).parent().prev().html("<a class=\"accept_task\">Accept task</a>");
		$(this).parent().html("Not selected");
		
		// Shrani release za task name
	});
	
	$(".edit_tasks").click(function (event) {
		event.preventDefault();
		$("#tasks_dialog").dialog("open");
	});
	
	$( "#tasks_dialog" ).dialog({
		autoOpen: false,
		open: function(event, ui) {
			$("input").blur();
		},
	    height: 800,
	    width: 650,
	    modal: true,
	    buttons: {
	    	"Save": function() {
	         	// Ne naredi nic, ker itak shranjujemo sproti
	         	
	            $(this).dialog("close");
	        }
	    },
	    close: function() {
	    	$("#trigger_id").attr('id', '');
	        allFields.val("");
	    }
	});
	
	$(".delete_task").click(function (event) {
		event.preventDefault();
		var taskNameToDelete = $(this).parent().parent().find(".task_name").val();
		
		// Delete task with name
		
		$(this).parent().parent().remove();
	});
	
	$(".task_name").focusout(function (event) {
		var newName = $(this).val();
	
		// Shrani new task name
	});
	
	$(".task_member").focusout(function (event) {
		var taskNameToSave = $(this).parent().parent().find(".task_name").val();
	
		// Shrani member for task name
	});
	
	$(".task_estimate").focusout(function (event) {
		var taskNameToSave = $(this).parent().parent().find(".task_name").val();
		
		// Shrani estimate for task name
	});
	
	$("#tasks_submit_new").click(function (event) {
		event.preventDefault();
		var taskNameToAdd = $("#tasks_input_description").val();
		
		var tableRow = "<tr>\
			<td><input class=\"task_name\" type=\"text\" value=\""+taskNameToAdd+"\"/></td><td><a class=\"delete_task\">delete</a></td><td>\
				<select class=\"task_member\">\
					<option value=\"\" disabled selected>member</option>\
					<option value=\"1\">gkolenc</option>\
					<option value=\"2\">gurh</option>\
					<option value=\"3\">akunovar</option>\
				</select>\
			</td><td><input class=\"task_estimate\" type=\"text\" name=\"time\"/></td>\
		</tr>";
		
		$("#tasks_dialog_table").append(tableRow);
		$("#tasks_input_description").val("");
		
		// Add task with name to DB
	});
	
	$('.confirm_story_submit').click(function (event) {
		event.preventDefault();
		
		var storyId = $(this).parent().parent().find("input:hidden").val();
		
		// Confirm stort as finished for this id
			
		$(this).parent().parent().remove();
	});
	
	$('.reject_story_submit').click(function (event) {
		event.preventDefault();
		
		var storyId = $(this).parent().parent().find("input:hidden").val();
		
		// Mark this story as unfinished and put it back to product backlog
			
		$(this).parent().parent().remove();
	});
	
	// =========================================================
	// Work time
	// =========================================================
	
	$(".show_work_time").click(function (event) {
		event.preventDefault();
		$(this).attr('id', 'trigger_id');
		
		var task_id = $(this).parent().parent().find("input:hidden").val();
		
		// Zgradi tabelo v dialogu za case za ta task id
		
		$("#task_work").dialog("open");
	});
	
	$( "#task_work" ).dialog({
		autoOpen: false,
		open: function(event, ui) {
			$("input").blur();
		},
	    height: 800,
	    width: 650,
	    modal: true,
	    buttons: {
	    	"Save": function() {
	         	var task_id = $("#trigger_id").parent().parent().find("input:hidden").val();
	         	
	         	// Shrani nove case za ta task id
	         	
	            $(this).dialog("close");
	        }
	    },
	    close: function() {
	    	$("#trigger_id").attr('id', '');
	        allFields.val("");
	    }
	});
	
	// =========================================================
	// Project wall
	// =========================================================
	
	$(".delete_post").click(function (event) {
		event.preventDefault();
		$(this).attr('id', 'trigger_id');
		$("#delete_post_dialog").dialog("open");
	});
	
	$("#delete_post_dialog").dialog({
		autoOpen: false,
	    height: 200,
	    width: 650,
	    modal: true,
	    buttons: {
	    	"Delete": function() {
	         	var post_id = $("#trigger_id").parent().parent().parent().find("input:hidden").val();
	         	
	         	// Izbrisi iz baze in odstrani iz html
	         	
	         	$("#trigger_id").parent().parent().parent().remove();
	         	
	            $(this).dialog("close");
	        },
	        Cancel: function() {
	          	$(this).dialog("close");
	        }
	    },
	    close: function() {
	    	$("#trigger_id").attr('id', '');
	        allFields.val("");
	    }
	});
});